var Mock = require("mockjs");
const patient = ['健康', '冠心病', '高血压', '气管炎', '偏头痛', '感冒', '支气管扩张', '肺炎'];
var Random = Mock.Random;

const guangdongPlaces = [
  "广州市",
  "深圳市",
  "珠海市",
  "汕头市",
  "佛山市",
  "江门市",
  "湛江市",
  "茂名市",
  "肇庆市",
  "惠州市",
  "梅州市",
  "汕尾市",
  "河源市",
  "阳江市",
  "清远市",
  "东莞市",
  "中山市",
  "潮州市",
  "揭阳市",
  "云浮市"
];

let users = []
const count = 20
const tokens = []
for (let i = 0; i < count; i++) {
  users.push(
    Mock.mock({
      id: i,
      ID: 'token' + i + Mock.Random.guid().slice(0, 13),
      name: Mock.Random.cname(),
      nickName:'@last',
      title: Mock.Random.cname(),
      age: Random.integer(20, 50),
      sex: Mock.Random.integer(0, 1),
      addr: '广东省 ' + guangdongPlaces[Math.floor(Math.random() * guangdongPlaces.length)],
      // addr: `${Random.province()}-${Random.city()}-${Random.county()}`,
      birth: Random.datetime("yyyy-MM-dd"),  // 值是指定格式的日期字符串
      confirm: Math.floor(Math.random() * 100 + 1),
      patientindex: patient[Math.floor(Math.random() * patient.length)],
    })
  )
  // console.log(users[1]);
  tokens.push(users[i].ID)
}
let user1 = toObject(tokens, users);
function toObject(names, values) {
  var result = {};
  for (var i = 1; i < names.length; i++)
    result[names[i]] = values[i];
  return result;
}
// console.log(user1);

export default {
  login: (config) => {
    const { username } = JSON.parse(config.body);
    const token = tokens[username];
    if (!token) {
      return {
        status: 1,
        message: "用户名或密码错误",
      };
    }
    return {
      status: 0,
      token,
    };
  },
  userInfo: (config) => {
    const token = config.body;
    const userInfo = user1[token];
    if (!userInfo) {
      return {
        status: 1,
        message: "获取用户信息失败",
      };
    }
    return {
      status: 0,
      userInfo,
    };
  },
  getUsers: () => {
    return {
      status: 0,
      users: Object.values(user1),
    };
  },
  deleteUser: (config) => {
    const { id } = JSON.parse(config.body);
    // console.log(id);
    const token = tokens[id];
    // console.log(tokens[id]);
    // console.log(users);
    if (token) {
      delete user1[token]
      delete tokens[id];
    }
    return {
      status: 0,
    };
  },
  editUser: (config) => {
    const data = JSON.parse(config.body);
    const { id } = data;
    const token = tokens[id];
    if (token) {
      user1[token] = { ...user1[token], ...data };
    }
    return {
      status: 0,
    };
  },
  ValidatUserID: (config) => {
    const userID = config.body;
    const token = tokens[userID];
    if (token) {
      return {
        status: 1,
      };
    } else {
      return {
        status: 0
      };
    }
  },
  addUser: (config) => {
    const data = JSON.parse(config.body);
    const { id } = data;
    tokens[id] = `${id}-token`
    user1[`${id}-token`] = {
      ...user1[`${id}-token`],
      ...data
    }
    return {
      status: 0,
    };
  },
  logout: (_) => {
    return {
      status: 0,
      data: "success",
    };
  },
};
